package com.zmj.sy.mom.srv.aps.service;

import cn.hutool.core.collection.CollUtil;
import com.zmj.sy.mom.srv.aps.bean.entity.mes.MatchDetailTask;
import com.zmj.sy.mom.srv.aps.bean.entity.mes.PartPallet;
import com.zmj.sy.mom.srv.aps.bean.entity.order.ApsWorkDetail;
import com.zmj.sy.mom.srv.aps.bean.vo.partpallet.PartPalletListReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.partpallet.PartPalletListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.partpallet.PartPalletAllMoveReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.partpallet.PartPalletReqVo;
import com.zmj.sy.mom.srv.aps.mapper.ApsWorkDetailMapper;
import com.zmj.sy.mom.srv.aps.mapper.MatchTaskDetailMapper;
import com.zmj.sy.mom.srv.aps.mapper.PartMapper;
import com.zmj.sy.mom.srv.aps.mapper.PartPalletMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class PartPalletService {

    private final PartPalletMapper partPalletMapper;
    private final PartMapper partMapper;
    private final MatchTaskDetailMapper matchTaskDetailMapper;
    private final ApsWorkDetailMapper apsWorkDetailMapper;
    @Transactional
    public List<PartPalletListResVo> list(PartPalletListReqVo reqVo) {

        List<PartPallet> list = partPalletMapper.lambdaQuery()
                .eq(PartPallet::getPositionCode, reqVo.getStationCode())
                .list();

        return  list.stream()
                .map(e-> {
                    PartPalletListResVo pp = new PartPalletListResVo();
                    pp.setPartCount(e.getPartCount());
                    pp.setPlmid(e.getPlmId());
                    return pp;
                })
                .collect(Collectors.toList());
    }

    @Transactional
    public void move(PartPalletAllMoveReqVo reqVo) {
        String[] split = {reqVo.getSourceCode()};
        if(reqVo.getSourceCode().contains(",")){
            split = reqVo.getSourceCode().split(",");
        }

        List<String> plmIdList = new ArrayList<>();

        for (String s : split) {
            List<PartPallet> partPalletList = partPalletMapper.lambdaQuery()
                    .eq(PartPallet::getPositionCode, s.trim())
                    .eq(PartPallet::getType, reqVo.getSourceType())
                    .list();

            for (PartPallet partPallet : partPalletList) {
                partPallet.setType(reqVo.getTargetType());
                partPallet.setPositionCode(reqVo.getTargetCode());
                partPalletMapper.updateById(partPallet);
                plmIdList.add(partPallet.getPlmId());
            }
        }

        if(CollUtil.isNotEmpty(plmIdList)){
            matchTaskDetailMapper.lambdaUpdate()
                    .in(MatchDetailTask::getMaterCode, plmIdList)
                    .eq(MatchDetailTask::getMatchCode, reqVo.getMatchCode())
                    .eq(MatchDetailTask::getStatus, 1)
                    .set(MatchDetailTask::getStatus, 2)
                    .update();
        }

    }

    public void updateUniqueCode(PartPalletReqVo reqVo) {
        apsWorkDetailMapper.lambdaUpdate().eq(ApsWorkDetail::getMaterNo, reqVo.getMaterNo().trim())
                .eq(ApsWorkDetail::getUniqueCode, reqVo.getUniqueCode().trim())
                .eq(ApsWorkDetail::getMaterName, reqVo.getMaterName().trim())
                .set(ApsWorkDetail::getUniqueCode, reqVo.getNewUniqueCode().trim())
                .set(ApsWorkDetail::getMaterName, reqVo.getNewMaterName().trim()).update();
        partPalletMapper.lambdaUpdate().eq(PartPallet::getPlmId, reqVo.getMaterNo().trim())
                .eq(PartPallet::getUniqueCode, reqVo.getUniqueCode().trim())
                .set(PartPallet::getUniqueCode, reqVo.getNewUniqueCode().trim())
                .update();
    }
}
